﻿using HtmlAgilityPack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Ozner.Common;
using System;
using System.Collections.Generic;
using System.Text;

namespace Ozner.BLL
{
    public class BaseBLL
    {

        //判断是否登录页面
        public bool IsLogin(HtmlAgilityPack.HtmlDocument doc)
        {



            return false;
        }

        /// <summary>
        /// 获取已经登录的json数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="uRL"></param>
        /// <returns></returns>
        protected BaseResult<T> PostNoLoginedJson<T>(string URL, string postdata)
        {
            return GetNoLoginedInfo<T>(URL, "POST", "JSON", postdata);
        }
        /// <summary>
        /// 获取已经登录的json数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="uRL"></param>
        /// <returns></returns>
        protected BaseResult<T> PostLoginedJson<T>(string URL,string postdata)
        {
            return GetLoginedInfo<T>(URL,"POST","JSON",postdata);
        }
        /// <summary>
        /// 获取已经登录的页面的HtmlDocument
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected BaseResult<T> GetLoginedDocument<T>(string URL)
        {
            return GetLoginedInfo<T>(URL, "GET", "HTML", null);
        }
        private BaseResult<T> GetLoginedInfo<T>(string URL, string method, string type, string postdata)
        {
            return GetInfo<T>(URL, method, type, postdata, true);
        }
        private BaseResult<T> GetNoLoginedInfo<T>(string URL, string method, string type, string postdata)
        {
            return GetInfo<T>(URL, method, type, postdata, false);
        }
        private BaseResult<T> GetInfo<T>(string URL,string method,string type,string postdata,bool isValidateLogin)
        {
            //处理url
            if (!URL.StartsWith("http"))
            {
                if (URL.StartsWith("/"))
                {
                    URL = ConfigModel.BaseUrl + URL;
                }
                else
                {
                    URL = ConfigModel.BaseUrl + "/" + URL;
                }
            }
            BaseResult<T> bsv = new BaseResult<T>()
            {
                Code = Code.NULL,
                Message = "网络连接失败!"
            };
            HttpBaseResult hbr = null;
            method = method.ToUpper();
            type = type.ToUpper();
            if (method == "GET")
            {
                if (type == "HTML")
                {
                    hbr = HttpHelper.HttpGetHtml(URL, ConfigModel.Cookies);
                }
                if (type == "JSON")
                {
                    hbr = HttpHelper.HttpGetJson(URL, ConfigModel.Cookies);
                }
            }
            if (method == "POST")
            {
                if (type == "HTML")
                {
                    hbr = HttpHelper.HttpPostHtml(URL, ConfigModel.Cookies, postdata);
                }
                if (type == "JSON")
                {
                    hbr = HttpHelper.HttpPostJson(URL, ConfigModel.Cookies, postdata);
                }
            }
            //判断结果
            if (hbr.Code == 200 && !string.IsNullOrEmpty(hbr.Html))
            {
                bsv.ResponseCookies = hbr.Cookie;
                if (type.ToUpper() == "HTML")
                {
                    HtmlDocument doc = new HtmlDocument();
                    doc.LoadHtml(hbr.Html);
                    if (isValidateLogin && IsLogin(doc))
                    {
                        bsv.Code = Code.LOGOUT;
                        bsv.Message = "您已下线，请先登录!";
                    }
                    else
                    {
                        bsv.Code = Code.OK;
                        bsv.OtherInfo = doc;
                    }
                }else
                {
                    bsv.Code = Code.OK;
                    bsv.Object = JsonConvert.DeserializeObject<T>(hbr.Html);
                }
            }
            return bsv;

        }
    }
}
